package com.yn.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yn.pet.bus.dao.GenFenceDao;
import com.yn.pet.bus.entity.GenFence;
import com.yn.pet.service.GenFenceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 地理围栏信息表(GenFence)表服务实现类
 *
 * @author makejava
 * @since 2025-07-09 12:00:47
 */
@Service("genFenceService")
public class GenFenceServiceImpl extends ServiceImpl<GenFenceDao, GenFence> implements GenFenceService {

    /**
     * 根据id查询围栏信息
     * @param fenceId 围栏id
     * @return
     */
    @Override
    public GenFence selectFenceById(Long fenceId) {
        if(fenceId == null){
            return null;
        }
        LambdaQueryWrapper<GenFence> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(GenFence::getFenceId, fenceId);
        lambdaQuery.eq(GenFence::getIsDelete, 0);
        return this.getOne(lambdaQuery);
    }

    /**
     * 修改围栏消息状态
     * @param fenceId 围栏id
     * @param messageStatus 状态
     */
    @Override
    public void updateMsgStatusById(Long fenceId, int messageStatus,boolean delete) {
        LambdaUpdateWrapper<GenFence> lambdaUpdate = Wrappers.lambdaUpdate();
        lambdaUpdate.eq(GenFence::getFenceId, fenceId);
        lambdaUpdate.set(GenFence::getMessageStatus, messageStatus);
        if(delete){
            lambdaUpdate.set(GenFence::getIsDelete, 1);
        }
        this.update(lambdaUpdate);
    }

    /**
     * 批量修改围栏消息状态
     * @param fenceIdList 围栏id列表
     * @param messageStatus 状态
     */
    @Override
    public void updateMsgStatusByIds(List<Long> fenceIdList, int messageStatus,boolean delete) {
        LambdaUpdateWrapper<GenFence> lambdaUpdate = Wrappers.lambdaUpdate();
        lambdaUpdate.in(GenFence::getFenceId, fenceIdList);
        lambdaUpdate.set(GenFence::getMessageStatus, messageStatus);
        if(delete){
            lambdaUpdate.set(GenFence::getIsDelete, 1);
        }
        this.update(lambdaUpdate);
    }

    @Override
    public void deleteById(Long fenceId) {
        if(fenceId == null){
            return;
        }
        LambdaUpdateWrapper<GenFence> lambdaUpdate = Wrappers.lambdaUpdate();
        lambdaUpdate.eq(GenFence::getFenceId, fenceId);
        this.remove(lambdaUpdate);
    }

    @Override
    public void deleteByIds(ArrayList<Long> fenceIds) {
        if(CollectionUtils.isEmpty(fenceIds)){
            return;
        }
        LambdaUpdateWrapper<GenFence> lambdaUpdate = Wrappers.lambdaUpdate();
        lambdaUpdate.in(GenFence::getFenceId, fenceIds);
        this.remove(lambdaUpdate);
    }


}

